package com.stephenone.zhdaily.homepage.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asksira.loopingviewpager.LoopingViewPager;
import com.rd.PageIndicatorView;
import com.stephenone.zhdaily.R;
import com.stephenone.zhdaily.data.bean.News;
import com.stephenone.zhdaily.data.bean.TopStories;
import com.stephenone.zhdaily.homepage.TopStoryAdapter;

import java.util.ArrayList;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by Administrator on 2018/1/29.
 */

public class HeaderViewBinder extends ItemViewBinder<TopStories,HeaderViewBinder.ViewHolder> {

    private Context mContext;

    public HeaderViewBinder(Context context){
        mContext = context;
    }
    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_news_header, parent,
                false);
        return new HeaderViewBinder.ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull TopStories item) {
        holder.mViewPager.setAdapter(new TopStoryAdapter(mContext, (ArrayList<News.TopStory>) item.getTopStoryList(), true));
        holder.mPageIndicatorView.setCount(holder.mViewPager.getIndicatorCount());
        holder.mViewPager.setIndicatorPageChangeListener(new LoopingViewPager.IndicatorPageChangeListener() {
            @Override
            public void onIndicatorProgress(int selectingPosition, float progress) {
                holder.mPageIndicatorView.setProgress(selectingPosition, progress);
            }

            @Override
            public void onIndicatorPageChange(int newIndicatorPosition) {

            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private LoopingViewPager mViewPager;
        private PageIndicatorView mPageIndicatorView;
        public ViewHolder(View itemView) {
            super(itemView);
            mViewPager = itemView.findViewById(R.id.viewpager);
            mPageIndicatorView = itemView.findViewById(R.id.indicator);
        }
    }
}
